/**
 * Created by lijiawei on 2018/10/30.
 */

var board = new Array();
var score = 0;
$(document).ready(function () {
    newgame();
  $('#reStart').click(function () {
    console.log(456456)
    $('.final').slideUp(500)
  })
});

function  newgame() {
    //初始化棋盘格
    init();
    //在随机的两个格子上生成数字
    produceOneNum();
    produceOneNum();
}

function init(){
    for( var i = 0 ; i < 4 ; i ++ )
        for( var j = 0 ; j < 4 ; j ++ ){

            var gridCell = $('#grid-cell-'+i+j);
            gridCell.css('top', getPosTop( i , j ) );
            gridCell.css('left', getPosLeft( i , j ) );
        }

    for( var i = 0 ; i < 4 ; i ++ ){
        board[i] = new Array();
        for( var j = 0 ; j < 4 ; j ++ ){
            board[i][j] = 0;
        }
    }
//更新数字
    updateBoardView();
}
//动态添加 数字格
function  updateBoardView() {
    //清空
    $(".number-cell").remove();

    //根据新的数字值重画
    for(var i = 0;i<4;i++)
        for( var j = 0 ; j < 4 ; j ++ ){
            $("#container").append('<div class="number-cell" id="number-cell-'+i+'-'+j+'"></div>');
            var theNumberCell = $('#number-cell-'+i+'-'+j);

            if( board[i][j] == 0 ){
                theNumberCell.css('width','0px');
                theNumberCell.css('height','0px');
                theNumberCell.css('top',getPosTop(i,j) + 50 );
                theNumberCell.css('left',getPosLeft(i,j) + 50 );
            }
            else{
                 theNumberCell.css('width','100px');
                 theNumberCell.css('height','100px');
                theNumberCell.css('top',getPosTop(i,j));
                theNumberCell.css('left',getPosLeft(i,j));
                theNumberCell.css('background-color',getNumberBackgroundColor( board[i][j] ) );
                theNumberCell.css('color',getNumberColor( board[i][j] ) );
                theNumberCell.text( getNames(board[i][j]) );
            }
        }
}

//随机位置显示生成的随机数字
function produceOneNum() {

    //判断还有无位置
    if (nospace (board) )
        return false;

    //随机一个位置
    var randx = parseInt(Math.floor( 4 * Math.random()));
    var randy = parseInt(Math.floor( 4 * Math.random()));

    while (true){
        if( board[randx][randy] == 0) break;
        randx = parseInt(Math.floor(4*Math.random()));
        randy = parseInt(Math.floor(4*Math.random()));

    }
    //随机一个数字
    var randNumber = Math.random() < 0.5? 2:4;
    //在随机位置显示随机数字
    board[randx][randy] = randNumber;
    showNumberWithAnimation( randx , randy , randNumber );

    return true;
}

/*// javascript是一種事件响应机制,所以不必写for , while循环,只需写 keydown事件
 判断玩家按了 上下左右那个键,做出对应的事*/
//键盘事件

$(document).keyup(function (event) {
    switch ( event.keyCode ){
        case 37 :  //left
            if( moveLeft() ) {
                produceOneNum();
                isgameOver();
            }
            break;
        case 38 :  //up
            if( moveUp() ) {
                produceOneNum();
                isgameOver();
            }
            break;
        case 39 : //right
            if( moveRight() ) {
                produceOneNum();
                isgameOver();
            }
            break;
        case 40 : //down
            if( moveDown() ) {
                produceOneNum();
                isgameOver();
            }
            break;
        default : break;
    }
});
//判断游戏结束
function isgameOver() {
    if(nospace(board) && !canMoveLeft(board)&&!canMoveRight(board)&&!canMoveUp(board)&&!canMoveDown(board))
    {
        $('.final').fadeIn(1000)
        $('.finalpoint').text(score)
    }
    else{
        score+=100;
        $('#score').text(score)
    }
}

//move left
function  moveLeft() {
    //是否能移动
    if( !canMoveLeft(board))
        return false;
    //开始移动
    for(var i = 0; i < 4;i++)
        for(var j = 1; j < 4; j++)
        { 
            if( board[i][j] !==0)
            {
                for(var k = 0; k<j ;k++)
                {   //落脚点为0 无障碍
                    if (board[i][k] ===0 && noBlockHorizontal(i , k ,j ,board) )
                    {
                        //move  
                        showMoveAnimation(i,j,i,k)
                        board[i][k] = board [i][j];
                        board[i][j] = 0;
                        continue  ;
                    }
                    //落脚点相等，无障碍
                   else if (board[i][k] ===board[i][j] && noBlockHorizontal(i , k ,j ,board))
                    {
                        //move
                        showMoveAnimation(i,j,i,k)
                        //add
                        board[i][k] += board [i][j];
                        board[i][j] = 0;

                        continue;
                    }
                }
            }
        }

    setTimeout("updateBoardView()",100);
        return true
}
//move right
function  moveRight() {
    if(!canMoveRight(board))
        return false;

    for(var i = 0; i < 4 ; i ++)
        for(var j = 2 ; j >= 0 ; j -- ){
            if(board[i][j]!==0)
            {
                for(var k = 3; k>j;k--)
                {
                    if(board[i][k]==0 &&noBlockHorizontal(i , j ,k ,board) )    {
                        //move
                        showMoveAnimation(i,j,i,k)
                        board[i][k] = board[i][j]
                        board[i][j] = 0;
                        continue;
                    }
                    else if(board[i][k] == board[i][j] && noBlockHorizontal(i , j ,k ,board))
                    {
                        //move
                        showMoveAnimation(i,j,i,k)
                        //add
                        board[i][k] *=2;
                        board[i][j] = 0;
                        continue;
                    }
                }

            }
        }
    setTimeout("updateBoardView()",200);
    return true;

}
//move up
function moveUp() {
    if(!canMoveUp(board))
        return false;
    //move
    for(var j = 0; j < 4 ; j ++)
        for(var i = 1; i < 4 ; i ++)
        {
            if(board[i][j] != 0){
                for(var k = 0 ; k < i ; k++)
                {
                    if(board[k][j]==0 && noBlockVertical(j, k , i ,board) )
                    {
                        //move
                        showMoveAnimation(i,j,k,j)
                        board[k][j] = board[i][j]
                        board[i][j] = 0;
                        continue
                    }
                    else if(board[k][j] ===board[i][j] && noBlockVertical( j  ,k , i ,board))
                    {
                        //move
                        showMoveAnimation(i,j,k,j)
                        //add
                        board[k][j] += board [i][j];
                        board[i][j] = 0;
                        continue;
                    }
                }
            }
        }
    setTimeout("updateBoardView()",200)
    return true;
}
//move down
function moveDown() {
    if(!canMoveDown(board))
        return false
    //move
    for(var j = 0; j < 4; j ++)
        for(var i = 2; i >= 0;i --){
            if(board[i][j] != 0) {
                for(var k = 3 ; k>i; k--)
                {
                    if(board[k][j]==0 &&noBlockVertical(j, i , k,board) )
                    {
                        //move
                        showMoveAnimation(i,j,k,j)
                        board[k][j] = board[i][j]
                        board[i][j] = 0;
                        continue
                    }
                    else if(board[k][j] ===board[i][j] && noBlockVertical( j, i  , k , board))
                    {
                        //move
                        showMoveAnimation(i,j,k,j)
                        //add
                        board[k][j] += board [i][j];
                        board[i][j] = 0;

                        continue;
                    }
                }
            }
        }
    setTimeout("updateBoardView()",200)
    return true
}